import json

from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse, HttpResponseForbidden, HttpResponseNotFound

# Create your views here.
from django.urls import reverse


def hello(request):
    return HttpResponse('你好!人间理想!')


# 路由传参
def index(request,temp,city):
    return HttpResponse('城市名:%s,温度:%s' %(city, temp))


# get请求
def get_view(request):
    name = request.GET.get('name')
    names = request.GET.getlist('name')
    age = request.GET.get('age')
    return HttpResponse('get:%s,%s,%s' % (name, names, age))


# post请求
def post_view(request):
    # 表单
    # name=request.POST.get('name')
    # age=request.POST.get('age')

    # json格式
    # 1.获取body数据
    body_data = request.body.decode()
    # print('body_data:',body_data)

    # 解析json数据，并且转换成字典格式
    body_dict = json.loads(body_data)

    # 获取数据
    name = body_dict.get('name')
    # print('222',name)
    age = body_dict.get('age')
    return HttpResponse('%s,%s' % (name, age))


# put请求
def put_view(request):
    # 获取body，并且转码
    body_data = request.body.decode()

    # 解析json数据 转化字典格式
    body_dict = json.loads(body_data)

    # 获取数据
    name = body_dict.get('name')
    age = body_dict.get('age')
    return HttpResponse('%s,%s' % (name, age))


# put请求
def delete_view(request):
    # 获取body，并且转码
    body_data = request.body.decode()

    # 解析json数据 转化字典格式
    body_dict = json.loads(body_data)

    # 获取数据
    name = body_dict.get('name')
    age = body_dict.get('age')
    return HttpResponse('%s,%s' % (name, age))


def request_view(request):
    print(request.method)  # result:对应的请求方式
    print(request.user)  # result:AnonymousUser
    print(request.COOKIES)
    return HttpResponse('request')


def response_view2(request):
    response = HttpResponse()
    response.content = '填入内容'
    # 这些都是默认的
    response.status_code = 200
    response['Content-Type'] = 'text/html;charset=utf-8'

    return response


def response_view(request):
    response = HttpResponse()
    res = {
        'name': '路人甲',
        'age': 20,
        'gender': '男'
    }
    response.status_code = 200
    # dumps是将dict转化成str格式，loads是将str转化成dict格式
    # json.dumps 序列化时对中文默认使用的ascii编码
    # ensure_ascii=False 不使用的ascii编码，以gbk编码
    response.content = json.dumps(res, ensure_ascii=False)

    return response


def json_response_view(request):
    d = {
        'name': 'luren',
        'age': 18
    }
    ds = [{
        'name': '路人甲',
        'age': 18
    },
        {
            'name': '路人乙',
            'age': 10
        }
    ]
    # return JsonResponse(d)
    return JsonResponse(ds,safe=False,json_dumps_params={'ensure_ascii':False})

    # d_json = json.dumps(d)
    # return HttpResponse(d_json)
    # return HttpResponse(ds)


# 禁止访问(403)
def forbiddent_view(request):
    return HttpResponseForbidden('拒绝访问')


# 没有找到(404)
def notfound_view(request):
    return HttpResponseNotFound('404')


# 重定向(301:永久重定向，302临时重定向)
def redirect_view(request):
    return redirect('/app01/hello/')


def redirect2_view(request):
    resp = HttpResponse()
    resp.status_code = 302
    resp['Location'] = '/app01/hello/'
    return resp


def user_center_view(request):
    # 通过name，反查路由
    url = reverse('app01:app1_login')
    return redirect(url)


def login_view(request):
    return HttpResponse('欢迎！')

#Cookie
def cookie_view(request):
    resp=HttpResponse('set cookie')
    resp.set_cookie('name','luren',max_age=20)
    return resp

def get_cookie_view(request):
    name=request.COOKIES.get('name')
    return HttpResponse('name:%s'% name)

#Session
def set_session_view(request):
    request.session['namee']='lurenj7'
    # request.session['name']='lurenj6'
    request.session['age']=19
    request.session.set_expiry(60)
    return HttpResponse('set session')

def get_session_view(request):
    name=request.session.get('name')
    return HttpResponse(name)

def del_sessoin_view(request):
    # request.session.pop('name')
    # del request.session['age']
    request.session.clear()
    return HttpResponse('del session')


def flush_session_view(request):
    request.session.flush()
    return HttpResponse('flush session')

